package cn.utils;

import cn.hutool.http.HttpRequest;
import cn.pojo.longitudeAndLatitude;
import cn.pojo.map;
import com.alibaba.fastjson.JSON;


import java.util.List;


public  class getLocation {
    private  String key="4e36c858f9eea612bfcc4a4c65510230";
    private String url="https://restapi.amap.com/v3/geocode/geo";




    public longitudeAndLatitude getLocation(String address){

        if (address==null && address.equals("")){
            return null;
        }

        url=url+"?"+"key="+key+"&address="+address;
//        System.out.println(url);
//        String forObject = rest.getForObject(url, String.class);

        String body = HttpRequest.get(url).execute().body();

//        System.out.println("-------------------");
//        System.out.println(body);
//        System.out.println("-------------------");
        map map = JSON.parseObject(body, cn.pojo.map.class);//总接受
//        System.out.println(map);
        List geocodes = map.getGeocodes();
//        System.out.println(geocodes.get(0));
        cn.pojo.geocodes geocodes1 = JSON.toJavaObject((JSON) geocodes.get(0), cn.pojo.geocodes.class);
        String location = geocodes1.location;
        String[] split = location.split(",");
        longitudeAndLatitude longitudeAndLatitude = new longitudeAndLatitude();
        longitudeAndLatitude.setLongitude(split[0]);
        longitudeAndLatitude.setLatitude(split[1]);
        return longitudeAndLatitude;

//        List<Map> listMaps = JSONArray.parseArray(JSON.toJSONString(geocodes.get(0)),Map.class);
//        System.out.println(listMaps);


//        JSON.parseObject(geocodes.get(0);, map.class);


    }
}
